<?php
class Fileclass
{
	var $searchfiles; 
	
	function del_dir($path,&$info = array(),&$err = array())  //delete all files in folder
	{
		global $user;
		$info = array();
		$info["file"]= 0;
		$info["dir"] = 0;
		if (!is_dir($path)) return false;
		
		$this->my_list($path,$dirs,$files);
		foreach($files as $f)
		{
			if (@unlink($f)) $info["file"]++;
			else $err.="cannot delete {$f} ";

		}
		for($i=count($dirs)-1;$i>=0;$i--)
		{
			$f = $dirs[$i];
			if (!@rmdir($f))
				$err.="cannot delete the directory {$f}\n";
			else
				$info["dir"]++;
		}
		if (@rmdir($path)) $info["dir"]++;
		return $info["dir"];
	}

	function copy($from,$to,$cover=false,$cut=false,&$coverfiles)
	{
		if (is_array($from) && is_array($to))
		{
			if (count($from)!=count($to)) return false;
			$all=true;
			for($i=0;$i<count($from);$i++)
			{
				if (!file_exists($from[$i])) continue;
				$this->move0($from[$i],$to[$i],$cover,$cut,$coverfiles) or $all=false;
			}
			return $all;
		}
		else
			return $this->move0($from,$to,$cover,$cut,$coverfiles);
	}
	
	function move0($from,$to,$cover,$cut,&$coverfiles)
	{
		if (is_file($from))
			return $this->move2($from,$to,$cover,$cut,$coverfiles);
		else if(is_dir($from))
			return $this->move1($from,$to,$cover,$cut,$coverfiles);
		else
			return false;
	}
	
	function move1($from,$to,$cover,$cut,&$coverfiles) 
	{
		if (!is_dir($to)) @mkdir($to,0777);
		$this->my_list($from,$dirs,$files,0);
		foreach($dirs as $d)
			$this->move1($d,str_replace($from,$to,$d),$cover,$cut,$coverfiles);
		foreach($files as $f)
			$this->move2($f,str_replace($from,$to,$f),$cover,$cut,$coverfiles);
		if ($cut) @rmdir($from); 
		return true;
	}
	
	function move2($from,$to,$cover,$cut,&$coverfiles)
	{
		if (!file_exists($from)) return false;
		if (file_exists($to))
		{
			if (!is_array($coverfiles)) $coverfiles=array();
			$coverfiles[]=$to;
			if ($cover)
			{
				@unlink($to);
				$this->move3($from,$to,$cut);
			}
		}
		else
			$this->move3($from,$to,$cut);
		return true;
	}
	
	function move3($from,$to,$cut)
	{
		return ($cut)?rename($from,$to):copy($from,$to);
	}

	function dir($path,&$dir,&$file,&$size,$deep=0) 
	{
		$this->my_list($path,$dir,$file,$deep);
		if (substr($path,-1)!="/") $path.="/";
		for($i=0;$i<count($dir);$i++)
		{
			$dir[$i]=str_replace($path,"",$dir[$i]);
		}
		sort($dir);
		sort($file);
		if ($size!="not")
		{
			$size=array();
			for($i=0;$i<count($file);$i++) $size[$i]=filesize($file[$i]);
		}
		for($i=0;$i<count($file);$i++)
			$file[$i]=str_replace($path,"",$file[$i]);
		return true;
	}
	
	function my_list($path,&$dir,&$file,$deepest=-1,$deep=0)
	{
		if(substr($path,-1)!="/") $path.="/";
		if (!is_array($file)) $file=array();
		if (!is_array($dir)) $dir=array();
		$handle=@opendir($path);
		while(($val=@readdir($handle)) !== false)
		{
			if ($val=='.' || $val=='..' || $val == '.svn') continue;
			$value = strval($path.$val);
			if (is_file($value))
				$file[]=$value;
			else if (is_dir($value))
			{
				$dir[]=$value;
				if ($deep<$deepest || $deepest==-1)
					$this->my_list($value."/",$dir,$file,$deepest,$deep+1);
			}
		}
		@closedir($handle);
		return true;
	}
	
	function search_file($path,$instr) 
	{
		$this->my_list($path,$dirs,$files);
		$return=array( 'file'=>array(), 'dir'=> array());
		$instr=strtolower($instr);
		
		foreach($files as $f)
		{
			$fname = strtolower(name_decode(basename($f)));
			if (strpos($fname,$instr) !== false )
			{
				$return['file'][]=$f;
				continue;
			}
		}
		
		foreach($dirs as $f)
		{
			if (substr($f,-1) == '/') $f = substr($f,0,-1);
			$fname = strtolower(name_decode(basename($f)));
			if (strpos($fname,$instr) !== false )
			{
				$return['dir'][]=$f;
				continue;
			}
		}
		
		return $return;
	}
	function get_property($path)
	{
		$this->my_list($path,$dirs,$files);
		$info = array();
		$info["dir"] = count($dirs);
		$info["file"] = count($files);
		$info["size"] = 0;
		foreach($files as $f)
			$info["size"]+=filesize($f);
		$info["writable"] = is_writable($path);
		return $info;
	}
}

